#   Turn this into a test. This puts code through
#   its paces by trawl the whole IPv4 address space ...

import os
import sys
import time

path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../netaddr'))
sys.path.insert(0, path)



from netaddr import CIDR

def cidr_iterator_speed_test():
    """
0.0.0.0         00000000.00000000.00000000.00000000 0.01 sec(s)
0.0.0.1         00000000.00000000.00000000.00000001 0.00 sec(s)
0.0.0.2         00000000.00000000.00000000.00000010 0.00 sec(s)
0.0.0.4         00000000.00000000.00000000.00000100 0.00 sec(s)
0.0.0.8         00000000.00000000.00000000.00001000 0.00 sec(s)
0.0.0.16        00000000.00000000.00000000.00010000 0.02 sec(s)
0.0.0.32        00000000.00000000.00000000.00100000 0.02 sec(s)
0.0.0.64        00000000.00000000.00000000.01000000 0.02 sec(s)
0.0.0.128       00000000.00000000.00000000.10000000 0.05 sec(s)
0.0.1.0         00000000.00000000.00000001.00000000 0.09 sec(s)
0.0.2.0         00000000.00000000.00000010.00000000 0.16 sec(s)
0.0.4.0         00000000.00000000.00000100.00000000 0.33 sec(s)
0.0.8.0         00000000.00000000.00001000.00000000 0.69 sec(s)
0.0.16.0        00000000.00000000.00010000.00000000 1.36 sec(s)
0.0.32.0        00000000.00000000.00100000.00000000 2.72 sec(s)
0.0.64.0        00000000.00000000.01000000.00000000 6.52 sec(s)
0.0.128.0       00000000.00000000.10000000.00000000 12.06 sec(s)
0.1.0.0         00000000.00000001.00000000.00000000 22.19 sec(s)
0.2.0.0         00000000.00000010.00000000.00000000 44.55 sec(s)
0.4.0.0         00000000.00000100.00000000.00000000 91.57 sec(s)
0.8.0.0         00000000.00001000.00000000.00000000 186.12 sec(s)
0.16.0.0        00000000.00010000.00000000.00000000 401.26 sec(s)
0.32.0.0        00000000.00100000.00000000.00000000 751.38 sec(s)
0.64.0.0        00000000.01000000.00000000.00000000 1431.89 sec(s)
0.128.0.0       00000000.10000000.00000000.00000000 2970.21 sec(s)

I gave up here but its interesting that this consistently uses about
5Mb of memory... The joy of generators!

1.0.0.0         00000001.00000000.00000000.00000000
2.0.0.0         00000010.00000000.00000000.00000000
4.0.0.0         00000100.00000000.00000000.00000000
8.0.0.0         00001000.00000000.00000000.00000000
16.0.0.0        00010000.00000000.00000000.00000000
32.0.0.0        00100000.00000000.00000000.00000000
64.0.0.0        01000000.00000000.00000000.00000000
128.0.0.0       10000000.00000000.00000000.00000000

IPy - IPv4
0.0.0.0         00000000000000000000000000000000 0.02 sec(s)
0.0.0.1         00000000000000000000000000000001 0.00 sec(s)
0.0.0.2         00000000000000000000000000000010 0.00 sec(s)
0.0.0.4         00000000000000000000000000000100 0.00 sec(s)
0.0.0.8         00000000000000000000000000001000 0.02 sec(s)
0.0.0.16        00000000000000000000000000010000 0.00 sec(s)
0.0.0.32        00000000000000000000000000100000 0.02 sec(s)
0.0.0.64        00000000000000000000000001000000 0.03 sec(s)
0.0.0.128       00000000000000000000000010000000 0.06 sec(s)
0.0.1.0         00000000000000000000000100000000 0.12 sec(s)
0.0.2.0         00000000000000000000001000000000 0.22 sec(s)
0.0.4.0         00000000000000000000010000000000 0.44 sec(s)
0.0.8.0         00000000000000000000100000000000 0.92 sec(s)
0.0.16.0        00000000000000000001000000000000 1.82 sec(s)
0.0.32.0        00000000000000000010000000000000 3.65 sec(s)
0.0.64.0        00000000000000000100000000000000 7.27 sec(s)
0.0.128.0       00000000000000001000000000000000 14.49 sec(s)

IPv6

::              0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000 0.01 sec(s)
::1             0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000001 0.00 sec(s)
::2             0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000010 0.00 sec(s)
::4             0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000100 0.02 sec(s)
::8             0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000001000 0.00 sec(s)
::10            0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000010000 0.02 sec(s)
::20            0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000100000 0.02 sec(s)
::40            0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000001000000 0.05 sec(s)
::80            0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000010000000 0.06 sec(s)
::100           0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000100000000 0.14 sec(s)
::200           0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000001000000000 0.31 sec(s)
::400           0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000010000000000 0.52 sec(s)
::800           0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000100000000000 1.09 sec(s)
::1000          0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0001000000000000 2.17 sec(s)
::2000          0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0010000000000000 4.32 sec(s)
::4000          0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0100000000000000 8.67 sec(s)
::8000          0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1000000000000000 17.29 sec(s)

IPy - IPv6

FAILED! No output produced ...
    """
    t1 = time.time()
    #for ip in CIDR('::/112'):
    for ip in CIDR('0.0.0.0/12'):
        i = int(ip)
        if i & (i - 1) == 0:
            delta = time.time() - t1
#            print "%-15s %s %.2f sec(s)" % (ip, ip.bits(), delta)
            print "%-15s %s %.2f sec(s)" % (ip, 'n/a', delta)
            t1 = time.time()

if __name__ == '__main__':
    import pycallgraph
    pycallgraph.start_trace()
    cidr_iterator_speed_test()
    pycallgraph.make_dot_graph('callgraph_IPv4.png')
